package com.chuanye.jiguangimdemo.activitys;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.chuanye.jiguangimdemo.R;
import com.chuanye.jiguangimdemo.adapters.ChatAdapter;
import com.chuanye.jiguangimdemo.app.MyApplication;

import java.util.List;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.content.MessageContent;
import cn.jpush.im.android.api.content.TextContent;
import cn.jpush.im.android.api.enums.ContentType;
import cn.jpush.im.android.api.event.MessageEvent;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.android.api.options.MessageSendingOptions;

public class ChatActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "ChatActivity";
    private Context mContext;
    private ImageView chat_back_iv;
    private TextView chat_title_tv;
    private ImageView chat_icon_iv;
    private TextView chat_edittext_et;
    private String title;
    private String targetID;
    private String targetAppKey;
    private Conversation conversation;
    private Button chat_send_btn;
    private ListView chat_listview_lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        mContext = this;

        Intent intent = getIntent();

        title = intent.getStringExtra(MyApplication.CONV_TITLE);
        targetID = intent.getStringExtra(MyApplication.TARGET_ID);
        targetAppKey = intent.getStringExtra(MyApplication.TARGET_APP_KEY);
        Log.i("打印传递来的目标的用户名：",""+targetID);


        initView();
    }

    private void initView() {
        //返回
        chat_back_iv = findViewById(R.id.chat_back_iv);
        chat_back_iv.setOnClickListener(this);
        //标题
        chat_title_tv = findViewById(R.id.chat_title_tv);
        //头像
        chat_icon_iv = findViewById(R.id.chat_icon_iv);
        chat_icon_iv.setOnClickListener(this);

        //输入框
        chat_edittext_et = findViewById(R.id.chat_edittext_et);
        //发送
        chat_send_btn = findViewById(R.id.chat_send_btn);
        chat_send_btn.setOnClickListener(this);

        //消息Listview
        chat_listview_lv = findViewById(R.id.chat_listview_lv);

        chat_title_tv.setText(title);

        //获取单个单聊会话
        conversation = JMessageClient.getSingleConversation(targetID, targetAppKey);
        if (conversation == null) {
            //创建单聊会话
            conversation = Conversation.createSingleConversation(targetID, targetAppKey);
        }

        List<Message> messageList = conversation.getAllMessage();
        for (int i = 0; i < messageList.size(); i++){
            if (ContentType.text == messageList.get(i).getContentType()){
                TextContent textContent = (TextContent) messageList.get(i).getContent();
                Log.i("打印消息222：",""+textContent.getText());
            }
        }
        for (Message message : messageList){
            TextContent textContent = (TextContent) message.getContent();
            Log.i("打印消息333：",""+textContent.getText());
        }

        ChatAdapter chatAdapter = new ChatAdapter(mContext, conversation);
        chat_listview_lv.setAdapter(chatAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.chat_back_iv://返回
                finish();
                break;
            case R.id.chat_icon_iv://头像
                 //如果是聊天室
               /* if (isChatRoom) {
                    startChatRoomActivity(getIntent().getLongExtra("chatRoomId", 0));
                } else {
                    //单聊
                    Intent intent = new Intent();
                    intent.putExtra(TARGET_ID, targetId);
                    intent.putExtra(TARGET_APP_KEY, appKey);
                    intent.putExtra(GROUP_ID, groupId);
                    intent.setClass(this, ChatDetailActivity.class);
                    startActivityForResult(intent, JGApplication.REQUEST_CODE_CHAT_DETAIL);
                    startChatDetailActivity(mTargetId, mTargetAppKey, mGroupId);
                }*/
                break;
            case R.id.chat_send_btn://发送
                String text = chat_edittext_et.getText().toString().trim();
                if (TextUtils.isEmpty(text)){
                    Toast.makeText(mContext,"",Toast.LENGTH_SHORT).show();
                }else {
                    //发送方式 1
                    //JMessageClient.createSingleTextMessage(targetID, targetAppKey, text);
                    //发送方式  2
                    TextContent textContent = new TextContent(text);
                    Message msg = conversation.createSendMessage(textContent);
                    //设置需要已读回执
                    MessageSendingOptions options = new MessageSendingOptions();
                    options.setNeedReadReceipt(true);
                    //附带控制参数的消息发送
                    JMessageClient.sendMessage(msg, options);

                    chat_edittext_et.setText("");
                }

                List<Message> messageList = conversation.getAllMessage();
                for (int i = 0; i < messageList.size(); i++){
                    if (ContentType.text == messageList.get(i).getContentType()){
                        TextContent textContent = (TextContent) messageList.get(i).getContent();
                         Log.i("打印消息：",""+textContent.getText());
                    }
                }
                break;
        }
    }


    /*public void onEvent(MessageEvent event){
        Log.i("接收到的对方的消息：","接收到的对方的消息。。。");
        final Message message = event.getMessage();
        TextContent textContent = (TextContent) message.getContent();
        Log.i("接收到的对方的消息：",""+ textContent.getText());
    }*/

    public void onEvent(MessageEvent event){
        Log.i("接收到的对方的消息：","接收到的对方的消息。。。");
        final Message message = event.getMessage();
        TextContent textContent = (TextContent) message.getContent();
        Log.i("接收到的对方的消息：",""+ textContent.getText());
    }
}
